package com.junyuan.fxf_pad;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.junyuan.fxf_pad.activity.fragment.FaceFragment;
import com.junyuan.fxf_pad.activity.fragment.MoreFragment;
import com.junyuan.fxf_pad.activity.fragment.MoveFragment;
import com.junyuan.fxf_pad.activity.fragment.MusicFragment;
import com.junyuan.fxf_pad.activity.fragment.VideoFragment;
import com.junyuan.fxf_pad.activity.fragment.VoiceFragment;
import com.junyuan.fxf_pad.adapter.NavMenuAdapter;
import com.junyuan.fxf_pad.entity.NavMenuItem;
import com.junyuan.fxf_pad.utils.BleUtils;
import com.junyuan.fxf_pad.widget.MyViewPager;
import com.junyuan.fxf_pad.widget.NoScrollListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    MyViewPager viewPager;
    MyFragmentAdapter fragmentAdapter;
    private Toolbar toolbar;
    TextView tvTitle;
    private DrawerLayout drawer;
    private NoScrollListView lv_nav_menu;
    private List<NavMenuItem> data;
    private NavMenuAdapter adapter;
    private String titles[] = {"移动", "语音", "视频", "表情", "音乐", "更多"};
    private int icons[] = {R.drawable.nav_move, R.drawable.nav_voice, R.drawable.nav_camera,
            R.drawable.nav_face, R.drawable.nav_music, R.drawable.nav_description};
    private int lastIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }



    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        lv_nav_menu = (NoScrollListView) findViewById(R.id.lv_nav_menu);

        data = getData();
        adapter = new NavMenuAdapter(this, data);
        lv_nav_menu.setAdapter(adapter);
        lv_nav_menu.setOnItemClickListener(this);

        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                Log.d("123",R.string.navigation_drawer_open+"");
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                Log.d("123",R.string.navigation_drawer_close+"");
            }
        };
//        drawer.setDrawerListener(toggle);
        drawer.addDrawerListener(toggle);

        toggle.syncState();


        viewPager = (MyViewPager) findViewById(R.id.viewpager);
        fragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdapter);

    }


    private List<NavMenuItem> getData() {
        List<NavMenuItem> data = new ArrayList<>();
        NavMenuItem bean;
        for (int i = 0; i < titles.length; i++) {
            bean = new NavMenuItem();
            bean.setIcon(icons[i]);
            bean.setTitle(titles[i]);
            if (i == lastIndex) {
                bean.setSelected(true);
            }
            data.add(bean);
        }
        return data;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position != lastIndex) {
            data.get(lastIndex).setSelected(false);
            data.get(position).setSelected(true);
            lastIndex = position;
            adapter.notifyDataSetChanged();

            tvTitle.setText(titles[position]);
            viewPager.setCurrentItem(position, false);
            drawer.closeDrawer(GravityCompat.START);
        } else {
            drawer.closeDrawer(GravityCompat.START);
        }
    }

    class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new MoveFragment();
                    break;
                case 1:
                    fragment = new VoiceFragment();
                    break;
                case 2:
                    fragment = new VideoFragment();
                    break;
                case 3:
                    fragment = new FaceFragment();
                    break;
                case 4:
                    fragment = new MusicFragment();
                    break;
                case 5:
                    fragment = new MoreFragment();
                    break;
            }
            return fragment;
        }


        @Override
        public int getCount() {
            return 6;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleUtils.getInstance().closeBle();
    }

}
